package com.sqin.taskflow;

import com.sqin.taskflow.exception.WorkflowException;
import com.sqin.taskflow.task.SingleTask;
import com.sqin.taskflow.task.SubMultiTask;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
public class MyCustomMultiJob extends SubMultiTask {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public MyCustomMultiJob(String name) {
        super(name);
    }

    @Override
    public Object execute(){

        Object lock = new Object();
        synchronized (lock){
            long waitTime= (long) (1000 + Math.random()*100);
            log.info(String.format("threadName: [%s] taskName:[%s] time:[%s]", Thread.currentThread().getName(), this.getName(), LocalDateTime.now().format(formatter)));
//            log.info("{}准备wait{}..",this.getName(),waitTime);
            try {
                lock.wait(waitTime);
                if (waitTime>1090){
                    throw new WorkflowException(this.getName()+"这是一个错误"+waitTime);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            log.info("结束");
        }
        return null;
    }

}
